﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HostpitalDevice
{
    using HostpitalDevice.Business;
    public partial class ConfigurationManage : Form
    {
        public ConfigurationManage()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ConfigurationManage_Load(object sender, EventArgs e)
        {
            DataTable result = SettingManage.GetAllType();
            if (result.Rows.Count > 0)
            {
                for (int i = 0; i < result.Rows.Count; i++)
                {
                    this.comboType.Items.Add(result.Rows[i][0].ToString());
                }
                this.comboType.SelectedIndex  = 0;
            }
        }

        private void comboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.listValue.Items .Clear();
            DataTable result = SettingManage.GetAllValueByType(comboType.SelectedItem.ToString());
            if (result.Rows.Count > 0)
            {
                for (int i = 0; i < result.Rows.Count; i++)
                {
                   this.listValue.Items.Add(result.Rows [i][0]);
                }
            }
        }

        private void btnDelType_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string type = comboType.SelectedItem.ToString();
                bool result = SettingManage.DelRecordByType(type );
                if (result == true)
                {
                    comboType.Items.Remove(comboType.SelectedItem);
                    listValue.Items.Clear();
                    this.comboType.SelectedIndex = 0;
                }
            }
        }

        private void btnAddType_Click(object sender, EventArgs e)
        {
            this.comboType.Items.Add(this.textType.Text.Trim());
            this.comboType.SelectedIndex = this.comboType.Items.Count - 1;
            this.textType.Text = "";
        }

        private void btnUpdateType_Click(object sender, EventArgs e)
        {
            string newType = this.textType.Text.Trim();
            string oldType = this.comboType.SelectedItem.ToString();
            bool result = SettingManage.UpdaeType(oldType, newType);
            if (result == true)
            {
                comboType.Items.Remove(comboType.SelectedItem);
                comboType.Items.Add(newType);
                this.comboType.SelectedIndex= this.comboType.Items.Count - 1;
            }
        }

        private void listValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.textValue.Text = this.listValue.SelectedItem.ToString();
            string memo = this.comboType.SelectedItem.ToString();   
        }

        private void btnAddValue_Click(object sender, EventArgs e)
        {
            string type = this.comboType.SelectedItem.ToString();
            string value = this.textValue.Text.Trim().ToString();
            if (SettingManage.Add(type,value ))
            {
                this.listValue.Items.Add(value);
                this.textValue.Text = "";
            }
        }

        private void btnDelValue_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string type = comboType.SelectedItem.ToString();
                string value = this.listValue.SelectedItem.ToString ();
                if (value.Equals(""))
                    return;
                bool result = SettingManage.DelRecord(type,value );
                if (result == true)
                {
                    listValue.Items.Remove(listValue.SelectedItem);
                    this.textValue.Text ="";
                }
            }
        }
    }
}